package org.szgb.console.sys.file.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysFile extends BeanEntity<SysFile> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;
    private String subFix;
    private Long size;

    public SysFile() {
    }

    public SysFile(String id) {
        this.setId(id);
    }

    public SysFile(String name, String subFix, Long size) {
        this.name = name;
        this.subFix = subFix;
        this.size = size;
    }

    public SysFile(SysFile.SysFileFindDto sysLineFindDto) {
        this.setId(sysLineFindDto.getId());
        this.setName(sysLineFindDto.getName());
        this.setPageDto(sysLineFindDto.getPageDto());
    }

    public SysFile(SysFile.SysFileSaveDto sysFileSaveDto) {
        this.setName(sysFileSaveDto.name);
    }

    public SysFile.SysFileView toSysFileView() {
        return new SysFile.SysFileView(this);
    }

    @Data
    @Schema(title = "文件信息", description = "文件信息")
    public static class SysFileView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "文件后缀")
        private String subFix;
        @Schema(description = "大小")
        private Long size;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysFileView(SysFile sysFile) {
            this.id = sysFile.getId();
            this.name = sysFile.getName();
            this.subFix = sysFile.getSubFix();
            this.size = sysFile.getSize();
            this.createdAt = sysFile.getCreatedAt();
            this.updatedAt = sysFile.getUpdatedAt();
            if (sysFile.getCreatedBy() != null) {
                this.createdById = sysFile.getCreatedBy().getId();
                this.createdByName = sysFile.getCreatedBy().getName();
            }
            if (sysFile.getUpdatedBy() != null) {
                this.updatedById = sysFile.getUpdatedBy().getId();
                this.updatedByName = sysFile.getUpdatedBy().getName();
            }
        }

        public static List<SysFile.SysFileView> getViewListFromList(List<SysFile> sysFileList) {
            return sysFileList.stream().map(SysFile.SysFileView::new).toList();
        }
    }

    @Data
    @Schema(title = "文件查询表单", description = "文件查询参数")
    public static class SysFileFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysFile toSysFile() {
            return new SysFile(this);
        }
    }

    @Data
    @Schema(title = "文件下载表单", description = "文件下载参数")
    public static class SysFileDownloadDto {
        @Schema(description = "ID")
        private String id;

        public SysFile toSysFile() {
            return new SysFile(this.getId());
        }
    }

    @Data
    @Schema(title = "文件保存表单", description = "文件保存参数")
    public static class SysFileSaveDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;

        public SysFile toSysFile() {
            return new SysFile(this);
        }
    }
}
